package com.woniuxy.factoryUtils;

import com.woniuxy.entity.Person;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;


/**
 * 三代工厂，希望直接通过配置文件获取对象！
 */
public class ThirdFactory {
    /**
     * 动态创建对应的类
     *
     * @return
     */
    public Person doPerson() {
        //创建properties对象，来解析properties文件
        Properties properties = new Properties();
        //二阶段输入流通过上面来读取？
        InputStream resourceAsStream = ThirdFactory.class.getResourceAsStream("doPerson.properties");
        String personName = "";
        Person person = null;
        try {
            //需要加载读取我们的配置文件
            properties.load(resourceAsStream);
            personName = properties.getProperty("person");
            //通过从配置文件中拿到的全限定类类名 来创建对象
            person = (Person) Class.forName(personName).newInstance();
            resourceAsStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return person;
    }
}
